//
//  JYTProCityZoneUrlHandler.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/11/23.
//  Copyright © 2016年 com.yuan. All rights reserved.
//

#import "JYTProCityZoneUrlHandler.h"
#import "JYTHttpClient.h"
#import "JYTCityAndServerUrl.h"
#import <MJExtension.h>
#import "JYTProvince.h"
#import "JYTCity.h"
#import "JYTZone.h"
#import "JYTUserManager.h"

@implementation JYTProCityZoneUrlHandler

+ (JYTProCityZoneUrlHandler *) shared
{
    static dispatch_once_t onceToken;
    static JYTProCityZoneUrlHandler *pczHandler;
    dispatch_once(&onceToken, ^{
        pczHandler = [[JYTProCityZoneUrlHandler alloc] init];
    });
    return pczHandler;
}


- (void) getCityAndAddressCompletionHandler:(void (^)(BOOL succ)) handler
{
    
    JYTHttpClient *client = [JYTHttpClient sharedInstance];
    
    client.urlType = userCenterUrl;
    
    client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg){
        
        NSDictionary *dic = result;
        
        NSArray * rarr = dic[@"data"];
        
        NSArray *arr = [JYTCityAndServerUrl mj_objectArrayWithKeyValuesArray:rarr];
        
        NSMutableArray *marr = [NSMutableArray arrayWithArray:arr];
        
        NSMutableArray *pronvicearr = [self componetArray:marr];
        
        [[JYTUserManager shared] setProviceArr:pronvicearr andcsuArr:marr];
        
        //        NSArray *ssss = [[UserManager shared] getCsuArr];
        //        ssss = [[UserManager shared] getProviceArr];
        
        handler(YES);
    };
    
    client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
        handler(NO);
    };
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //请求 参数
    
    //请求 CommandCode
    [param setObject:@"GetConfig" forKey:@"CommandCode"];
    
    [client post:@"GetDataInfo" withParam:param];
}

- (NSMutableArray *)componetArray:(NSMutableArray *)marr
{
    //选出省份
    NSMutableArray *parr = [NSMutableArray array];
    
    [self arrayDivideProvice:marr andNumber:@0 andHandler:^(NSMutableArray *pMarr, NSMutableArray *rpMarr) {
        [parr addObjectsFromArray:pMarr];
        //        [marr removeObjectsInArray:rpMarr];
    }];
    
    
    //选出市
    for (JYTProvince *pro in parr) {
        
        NSNumber *zone_id = pro.zone_id;
        NSMutableArray *cities = [NSMutableArray array];
        
        [self arrayDivideCity:marr andNumber:zone_id andHandler:^(NSMutableArray *cMarr, NSMutableArray *rcMarr) {
            [cities addObjectsFromArray:cMarr];
            //            [marr removeObjectsInArray:rcMarr];
        }];
        
        pro.cities = cities;
        
    }
    
    //选出区
    for (JYTProvince *pro in parr) {
        
        NSMutableArray *cmarr = [NSMutableArray arrayWithArray:pro.cities];
        
        for (JYTCity *city in cmarr) {
            
            
            NSNumber *zone_id = city.zone_id;
            NSMutableArray *zones = [NSMutableArray array];
            
            
            [self arrayDivideZone:marr andNumber:zone_id andHandler:^(NSMutableArray *zMarr, NSMutableArray *rzMarr) {
                [zones addObjectsFromArray:zMarr];
                //                [marr removeObjectsInArray:rzMarr];
            }];
            
            city.zones = zones;
            
        }
    }
    
    
    return parr;
}

//省份
- (void)arrayDivideProvice:(NSMutableArray *)marr andNumber:(NSNumber *)num andHandler:(void (^)(NSMutableArray *pMarr, NSMutableArray *rpMarr)) handler
{
    //省数组
    NSMutableArray *pMarr = [NSMutableArray array];
    NSMutableArray *rpMarr = [NSMutableArray array];
    
    for (JYTCityAndServerUrl *csu in marr) {
        
        if ([csu.parent_id isEqualToNumber:num]) {
            JYTProvince *proviec = [[JYTProvince alloc] initWith:csu];
            [pMarr addObject:proviec];
            [rpMarr addObject:csu];
        }
    }
    
    handler(pMarr, rpMarr);
}


//城市
- (void)arrayDivideCity:(NSMutableArray *)marr andNumber:(NSNumber *)num andHandler:(void (^)(NSMutableArray *cMarr, NSMutableArray *rcMarr)) handler
{
    //市数组
    NSMutableArray *cMarr = [NSMutableArray array];
    NSMutableArray *rcMarr = [NSMutableArray array];
    
    for (JYTCityAndServerUrl *csu in marr) {
        
        if ([csu.parent_id isEqualToNumber:num]) {
            JYTCity *city = [[JYTCity alloc] initWith:csu];
            [cMarr addObject:city];
            [rcMarr addObject:csu];
        }
    }
    
    handler(cMarr, rcMarr);
}


//城市
- (void)arrayDivideZone:(NSMutableArray *)marr andNumber:(NSNumber *)num andHandler:(void (^)(NSMutableArray *zMarr, NSMutableArray *rzMarr)) handler
{
    //市数组
    NSMutableArray *zMarr = [NSMutableArray array];
    NSMutableArray *rzMarr = [NSMutableArray array];
    
    for (JYTCityAndServerUrl *csu in marr) {
        
        if ([csu.parent_id isEqualToNumber:num]) {
            JYTZone *zone = [[JYTZone alloc] initWith:csu];
            [zMarr addObject:zone];
            [rzMarr addObject:csu];
        }
    }
    
    handler(zMarr, rzMarr);
}
@end
